#!/bin/bash
#backupAllDbs.sh
time="$(date +%Y%m%d%H%M%S)"
month="$(date +%Y%m)"
# delete files 30  days ago
rm -rf `find /var/nas/db_bak -mtime +30`

dir=/var/nas/db_bak/$time
test ! -d "$dir" && mkdir -p $dir
# modify root passwd
rootPasswd=bxn500
logfile=/var/nas/db_bak/backup-$month.log
test ! -f "$logfile" && touch $logfile
echo "====>> $time begin to backup " >> $logfile
dbs=`mysql -uroot -p"$rootPasswd" -e "show databases;"| grep -v Database|grep -v _schema`

for db in $dbs;do
        timeName=$(date +%Y%m%d%k%M%S)
        mysqldump -uroot -p"$rootPasswd" $db > /tmp/$db.$timeName.sql;
done
mv /tmp/*.sql $dir/
files=`ls $dir`
echo "$(date +%Y%m%d%k%M%S) backup end. files:  $files <<====" >> $logfile
